<template>
	<view class="columnbox wdh-100 content">
		<view class="rowbox spb wdh-100 top-box">
			<view class="rowbox topcont" v-if="logflag">
				<view class="left">
					<image class="user-img" src="../../static/img/tx.png" mode="aspectFill"></image>
					<view class="user-phone">用户ID：{{userinfo.id}}</view>
				</view>
				<!-- <view class="right">
					<view class="imgcont" @click="showmapop=true">
						<image class="img" src="../../static/img/dianma.png" mode=""></image>
					</view>
					
				</view> -->

			</view>
			<view class="rowbox" v-else @click="toLog">
				<image class="user-img" src="../../static/img/tx.png" mode="aspectFill"></image>
				<view class="user-phone">立即登录</view>
			</view>
		</view>
		<view class="columnbox wdh-100 one-info">
			<view class=" wdh-100 spb cont-info">
				<view class="info-title">
					<view class="label">
						微信商户收款名称
					</view>
		
					<view class="val">
						{{baseInfo.shname||'-'}}
					</view>
		
		
				</view>
				<view class="info-title">
					<view class="label">
						微信收款商户号
					</view>
					<view class="val">
						{{baseInfo.shh||'-'}}
					</view>
		
		
				</view>
			
			</view>
			
		</view>
		<view class="tool-box columnbox">
			<view class="tool-title">常用功能</view>
			<button class="rowbox wdh-100 spb one-tool" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
				v-if="!userinfo.phone">
				<view>授权手机号</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</button>
			<view class="rowbox wdh-100 spb one-tool" @click="handleShowMa">
				<view>我的二维码</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</view>
			<view class="rowbox wdh-100 spb one-tool" @click="showPackage">
				<view>我的会员</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</view>
			<button open-type="contact" class="rowbox wdh-100 spb one-tool">
				<view>联系客服</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</button>
			<view class="rowbox wdh-100 spb one-tool" @click="showSrva">
				<view>用户协议</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</view>
			<view class="rowbox wdh-100 spb one-tool" @click="showPrivacy">
				<view>隐私协议</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</view>
			<view class="rowbox wdh-100 spb one-tool" @click="quitLog">
				<view>退出登录</view>
				<u-icon name="arrow-right" size="28rpx"></u-icon>
			</view>
		</view>

		<u-popup round="8" :show="showmapop" @close="showmapop=false" mode="center">
			<view class="phonePop">
				<view class="tit">
					本店二维码
				</view>
				<view class="imgcont">
					<image :src="ma" mode="widthFix" class="img"></image>
				</view>
				<view class="footer-btn">
					<view class="btn confirmbtn" @click="handleDownMa">
						下载
					</view>
					<view class="btn" @click="showmapop=false">
						关闭
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showmapop: false,
				ma: '',
				isIos: true,
				userinfo: null,
				logflag: false,
				baseInfo:''
			};
		},
		onLoad() {
			if (uni.getStorageSync('uid')) {
				this.logflag = true
				this.getUserInfo()
			} else {
				this.logflag = false
			}
		},
		onShow() {
			const info = wx.getSystemInfoSync()
			if (!(info.system.indexOf('Android') > -1)) {
				this.isIos = false
			}
			if (uni.getStorageSync('uid') && !this.logflag) {
				this.logflag = true
				this.getUserInfo()
			}
			this.getStoreInfo()
		},
		methods: {
			handleShowMa() {
				// uni.showModal({
				// 	title: '提示',
				// 	content: '请先认证门店',
				// 	showCancel: true
				// })
				if (uni.getStorageSync('userInfo').shanghuid == 0) {
					uni.showToast({
						icon: 'none',
						title: '请先认证'
					})
					return
				}
				this.showmapop = true
			},
			getPhoneNumber(e) {
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					uid: uni.getStorageSync('uid')
				}
				this.$req.post('/shanghu/phoneJm', {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					uid: uni.getStorageSync('uid')
				}).then(res => {
					if (res.data.code == 0) {
						uni.showToast({
							title: '授权成功'
						})
						this.getUserInfo()
					} else {
						uni.hideLoading()
						uni.showModal({
							title: '授权手机号失败',
							content: '请稍后再试'
						})
					}
				})
			},
			handleDownMa() {
				this.downSaveImage(this.ma)
			},
			downSaveImage(imgurl) {
				uni.getSetting({
					success(res) {
						if (res.authSetting['scope.writePhotosAlbum']) {
							// 已授权，直接保存图片
							uni.downloadFile({
								url: imgurl,
								success: (res) => {
									if (res.statusCode === 200) {
										uni.saveImageToPhotosAlbum({
											filePath: res.tempFilePath,
											success: function() {
												uni.showToast({
													title: '已保存到相册',
													duration: 2000,
												})
											},
											fail: function() {
												uni.showToast({
													title: '保存失败，请稍后重试',
													icon: 'none',
												})
											},
										})
									}
								},
							})
						} else if (res.authSetting['scope.writePhotosAlbum'] === false) {
							// 用户已拒绝授权，提示用户授权
							uni.showModal({
								title: '提示',
								content: '您未授权保存图片到相册，是否前往设置页面进行授权？',
								success: function(res) {
									if (res.confirm) {
										uni.openSetting({
											success: function(res) {
												if (res.authSetting[
														'scope.writePhotosAlbum']) {
													uni.downloadFile({
														url: imgurl,
														success: (res) => {
															if (res
																.statusCode ===
																200) {
																uni.saveImageToPhotosAlbum({
																	filePath: res
																		.tempFilePath,
																	success: function() {
																		uni.showToast({
																			title: '已保存到相册',
																			duration: 2000,
																		})
																	},
																	fail: function() {
																		uni.showToast({
																			title: '保存失败，请稍后重试',
																			icon: 'none',
																		})
																	},
																})
															}
														},
													})
												}
											},
										})
									} else if (res.cancel) {
										uni.showToast({
											title: '您取消了授权',
											icon: 'none',
											duration: 2000,
										})
									}
								},
							})
						} else {
							// 用户第一次调用，调用授权接口
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									uni.downloadFile({
										url: imgurl,
										success: (res) => {
											if (res.statusCode === 200) {
												uni.saveImageToPhotosAlbum({
													filePath: res.tempFilePath,
													success: function() {
														uni.showToast({
															title: '保存成功',
															duration: 2000,
														})
													},
													fail: function() {
														uni.showToast({
															title: '保存失败，请稍后重试',
															icon: 'none',
														})
													},
												})
											}
										},
									})
								},
								fail() {
									uni.showToast({
										title: '授权失败，请稍后重试',
										icon: 'none',
										duration: 2000,
									})
								},
							})
						}
					},
				})
			},
			showPackage() {
				if (!uni.getStorageSync('uid')) {
					uni.navigateTo({
						url: '/pages/log/log'
					})
				} else {
					uni.navigateTo({
						url: '/pages/package/package'
					})
				}
			},
			showSrva() {
				uni.navigateTo({
					url: '/pages/proto/srvagmt/srvagmt'
				})
			},
			showPrivacy() {
				uni.navigateTo({
					url: '/pages/proto/privacypolicy/privacypolicy'
				})
			},
			toLog() {
				uni.navigateTo({
					url: '/pages/log/log'
				})
			},
			getUserInfo() {
				this.$req.post('/shanghu/userInfo', {
					uid: uni.getStorageSync('uid')
				}).then(res => {
					uni.removeStorageSync('userInfo')
					uni.setStorageSync('userInfo', res.data.data)
					this.userinfo = res.data.data

				})
			},
			getStoreInfo() {

				this.$req.post('/shanghu/shanghuInfo', {
					shanghuid: uni.getStorageSync('userInfo').shanghuid
				}).then(res => {
					this.ma = res.data.data.erweima
					this.baseInfo = res.data.data
				})
			},
			quitLog() {
				uni.clearStorageSync()
				this.logflag = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.info-title {
		display: flex;
		align-items: center;
		justify-content: space-between;
	
		padding: 20rpx 0;
		border-bottom: 1px solid #edf0f2;
	
		&:last-child {
			border: none;
		}
	
		// color: #6f6e70;
		.label {
			color: #999;
			font-size: 28rpx;
	
		}
	
		.val {
			font-size: 30rpx;
			font-weight: 500;
		}
	}
	.one-info {
		background: #fff;
		width: 100%;
		padding: 20rpx 32rpx;
		margin-top: -70rpx;
		border-radius: 20rpx;
		width: 710rpx;
	}
	button {
		margin: 0;
		padding: 0;
		line-height: 35rpx;
		background-color: transparent;
	}

	button::after {
		border: none;
	}

	.phonePop {
		width: 90vw;
		display: flex;
		flex-direction: column;
		// align-items: center;
		padding: 30rpx 24rpx 30rpx 24rpx;
		min-height: 500rpx;

		.imgcont {
			padding-top: 30rpx;
			width: 400rpx;
			margin: 0 auto;

			.img {
				display: block;
				width: 100%;
				height: 100%;
			}
		}

		.tit {
			font-size: 30rpx;
			font-weight: 500;
			flex-shrink: 0;
			text-align: center;
		}

		.name {
			font-size: 28rpx;
			margin-top: 30rpx;
			flex: 1;
			padding: 0 20rpx;
		}

		.namecenter {
			text-align: center;
		}



		.footer-btn {
			flex-shrink: 0;
			padding-top: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;

			.btn {
				line-height: 35rpx;
				width: calc(50% - 15rpx);
				padding: 20rpx;
				border-radius: 8rpx;
				font-size: 30rpx;
				font-weight: 500;
				text-align: center;
				background-color: #f5f5f5;
			}

			.confirmbtn {
				background-color: #57bd6a;
				color: #fff;
			}
		}

		.btncenter {
			justify-content: center;
		}
	}

	.topcont {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;

		.left {
			display: flex;
			align-items: center;
		}

		.right {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;

			.imgcont {
				width: 60rpx;
				height: 60rpx;

				.img {
					display: block;
					width: 100%;
					height: 100%;
				}
			}

			.text {
				margin-top: 5rpx;
			}
		}
	}

	.content {
		min-height: 100vh;
		background: #f6f6f6;
		justify-content: flex-start;
	}

	.top-box {
		padding: 20rpx 32rpx;
		padding-bottom: 100rpx;
		background: linear-gradient(to bottom, #57bd6a, #f6f6f6);
	}

	.user-img {
		width: 100rpx;
		height: 100rpx;
		border-radius: 100rpx;
	}

	.user-phone {
		margin-left: 20rpx;
		color: #313345;
		font-size: 36rpx;
	}

	.tool-box {
		background: #fff;
		margin-top:40rpx;
		width: 710rpx;
		border-radius: 10rpx;
		padding: 32rpx 32rpx;
	}

	.tool-title {
		color: #313345;
		font-size: 36rpx;
		width: 100%;
		border-bottom: #eeeeee solid 1px;
		padding-bottom: 20rpx;
	}

	.one-tool {
		color: #313345;
		font-size: 28rpx;
		margin-top: 20rpx;
		padding: 20rpx 0;
	}
</style>